import 'package:auto_orientation/auto_orientation.dart';
import 'package:bxultimate/widgets/video_controls/video_controls.dart';
import 'package:chewie/chewie.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

class AppVideoPlay extends StatefulWidget {
  final String url;
  final String title;

  const AppVideoPlay({super.key, required this.url, required this.title});

  @override
  State<AppVideoPlay> createState() => _AppVideoPlayState();
}

class _AppVideoPlayState extends State<AppVideoPlay> {
  VideoPlayerController? _controller;
  ChewieController? _chewieController;

  @override
  void initState()  {
    // TODO: implement initState
    super.initState();

    initController();
  }
  initController() async {
    _controller = VideoPlayerController.network(widget.url,
        videoPlayerOptions: VideoPlayerOptions(mixWithOthers: true));
    await _controller?.initialize();
    _chewieController = ChewieController(
        videoPlayerController: _controller!,
        autoPlay: true,
        allowedScreenSleep: false,
        playbackSpeeds: [0.5, 1, 1.25, 1.5, 2],
        customControls: VideoControls(
          title: widget.title,
          showBackBtn: false,
          // actions: _buildDownload(url, name),
        ),
        routePageBuilder: (BuildContext context, Animation<double> animation,
            Animation<double> secondAnimation, provider) {
          // 全屏时，横向视频自动旋转
          final videoWidth =
              provider.controller.videoPlayerController.value.size.width;
          final videoHeight =
              provider.controller.videoPlayerController.value.size.height;
          if (videoWidth > videoHeight) {
            AutoOrientation.landscapeAutoMode(forceSensor: true);
          }
          // 全屏时，横向视频自动旋转
          // chewie _defaultRoutePageBuilder
          return AnimatedBuilder(
            animation: animation,
            builder: (BuildContext context, Widget? child) {
              return Scaffold(
                resizeToAvoidBottomInset: false,
                body: Container(
                  alignment: Alignment.center,
                  color: Colors.black,
                  child: provider,
                ),
              );
            },
          );
        });

    setState(() {

    });
  }

  @override
  Widget build(BuildContext context) {
    return  AspectRatio(
      aspectRatio: 16 / 9,
      child: Container(
          color: Colors.black,
          child: _chewieController != null && _controller != null
              ? Chewie(
            controller: _chewieController!,
          )
              : Stack(
            children: const <Widget>[
              Align(
                alignment: Alignment.topLeft,
                child: BackButton(
                  color: Colors.white,
                ),
              ),
              Center(
                child: CircularProgressIndicator(),
              )
            ],
          )),
    );
  }


  void dispose() {
    _controller?.dispose();
    _chewieController?.dispose();
    super.dispose();
  }
}
